package org.mindrive.system.dao;

import java.util.List;
import java.util.stream.Collectors;

import org.mindrive.system.collection.system.RoleEntity;
import org.mindrive.system.collection.system.RolePermissionEntity;
import org.mindrive.system.dao.mapper.RoleMapper;
import org.mindrive.system.dao.mapper.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author houwei
 * @since 2021-10-14
 */
@Service
public class RoleDao extends ServiceImpl<RoleMapper, RoleEntity> {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	public int getCountByCode(String code) {
		QueryWrapper<RoleEntity> query = new QueryWrapper<RoleEntity>();
		query.eq("code", code);
		int count = roleMapper.selectCount(query);
		return count;
	}

	public RoleEntity getRoleByCode(String workspaceId, String code) {
		QueryWrapper<RoleEntity> query = new QueryWrapper<RoleEntity>();
		query.eq("code", code).eq("workspace_id", workspaceId);
		RoleEntity role = roleMapper.selectOne(query);
		return role;
	}

	public List<String> getRolePermissions(String roleId) {

		QueryWrapper<RolePermissionEntity> query = new QueryWrapper<RolePermissionEntity>();
		query.eq("role_id", roleId);
		List<RolePermissionEntity> list = rolePermissionMapper.selectList(query);

		List<String> permissions = list.stream().map(item -> item.getPermissionId()).collect(Collectors.toList());
		return permissions;

	}

}
